package ru.ipo.dces.problemsviewer;

import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.plaf.basic.BasicButtonUI;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * Created by IntelliJ IDEA.
 * User: ilya
 * Date: 02.06.11
 * Time: 13:14
 */
public class ProblemHeaderAddTabComponent extends JButton {

    private static final int SIZE = 16;
    private final JTabbedPane pane;

    public ProblemHeaderAddTabComponent(JTabbedPane pane) {

        this.pane = pane;

        setPreferredSize(new Dimension(SIZE, SIZE));
        setBorder(new LineBorder(Color.blue));
        setFocusable(false);
        setUI(new BasicButtonUI());
        setContentAreaFilled(false);
        setRolloverEnabled(true);

        setBorderPainted(false);

        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseExited(MouseEvent e) {
                setBorderPainted(false);
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                setBorderPainted(true);
            }
        });
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.black);
        if (getModel().isPressed())
            g.translate(1, 1);
        int padding = 4;
        ((Graphics2D)g).setStroke(new BasicStroke(2));
        g.drawLine(padding, SIZE / 2, SIZE - padding, SIZE / 2);
        g.drawLine(SIZE / 2, padding, SIZE / 2, SIZE - padding);
    }

    private int getTabIndex() {
        return pane.indexOfTabComponent(this);
    }

}
